﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AWA.VAT.EmailApp.Models;
using System.Windows.Forms;
using System.Diagnostics;
using AWA.VAT.EmailApp.Progress;
using AWA.VAT.DbAccess.Excel;

namespace AWA.VAT.EmailApp.Controllers
{
    public class VATEmailController
    {
        EmailForm _form;
        VATEmailModel _model;
        ProgressForm _progress;

        public event EventHandler<EmailEventArgs> EmailFailed
        {
            add { _model.EmailFailed += value; }
            remove { _model.EmailFailed -= value; }
        }

        public event EventHandler<EmailEventArgs> EmailSent
        {
            add { _model.EmailSent += value; }
            remove { _model.EmailSent -= value; }
        }

        public event EventHandler<EmailEventArgs> EmailSkipped
        {
            add { _model.EmailSkipped += value; }
            remove { _model.EmailSkipped -= value; }
        }

        public event EventHandler EmailingCompleted
        {
            add { _model.EmailingCompleted += value; }
            remove { _model.EmailingCompleted -= value; }
        }

        public event EventHandler AuthenticationFailed
        {
            add { _model.AuthenticationFailed += value; }
            remove { _model.AuthenticationFailed -= value; }
        }

        public VATEmailController()
        {
            this._form = new EmailForm(this);
            this._model = new VATEmailModel(); //new ExcelDataConnector(Configuration.FileLocation)
            this._form.Message = _model.BuildRawMessage();  // TODO: refactor this
            this._progress = new ProgressForm();
            this._progress.EnableCancel = false;

            this.EmailFailed += new EventHandler<EmailEventArgs>(VATEmailController_EmailFailed);
            this.EmailingCompleted += new EventHandler(VATEmailController_EmailSucceeded);
            this.EmailSent += new EventHandler<EmailEventArgs>(VATEmailController_EmailSent);
            this.EmailSkipped += new EventHandler<EmailEventArgs>(VATEmailController_EmailSkipped);
        }

        public void Execute()
        {
            _form.ShowDialog();
        }

        public void Send()
        {
            SaveCredentials();
            _form.SendEnabled = false;
            _progress.Reset(214);
            _progress.Show();
            _model.SendEmails(new ExcelDataConnector(Configuration.FileLocation));
            _progress.Hide();
            _form.SendEnabled = true;
        }

        public void SaveCredentials()
        {
            Configuration.EmailUsername = _form.Username;
            Configuration.EmailPassword = _form.Password;
        }

        public void OpenSpreadsheet()
        {
            OpenFileDialog dialog = new OpenFileDialog();

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                Configuration.FileLocation = dialog.FileName;
            }
        }

        #region Event Handlers
        private void VATEmailController_EmailSent(object sender, EmailEventArgs e)
        {
            _progress.ProgressText = String.Format("Email sent to {0} ({1})", e.Person.RealName, e.Person.Email);
            _progress.Step();
        }

        private void VATEmailController_EmailSkipped(object sender, EmailEventArgs e)
        {
            _progress.ProgressText = String.Format("Skipped email to {0} ({1}): already sent",
                e.Person.RealName, e.Person.Email);
            _progress.Step();
        }

        private  void VATEmailController_EmailSucceeded(object sender, EventArgs e)
        {
            MessageBox.Show("Email sends completed!");
        }

        private void VATEmailController_EmailFailed(object sender, EmailEventArgs e)
        {
            string message = e.Person.Email + " failed";
            Debug.WriteLine(message);
            MessageBox.Show(message);
        }
        #endregion
    }
}
